package com.oracle.team.service;

import java.util.ArrayList;

import com.oracle.team.domain.Architect;
import com.oracle.team.domain.Designer;
import com.oracle.team.domain.Employee;
import com.oracle.team.domain.PC;
import com.oracle.team.domain.Programmer;

public class TeamServiceColl {
	
	private static int counter = 1;	//累加器
    private final int MAX_MEMBER = 6;
    //private Programmer[] team = new Programmer[MAX_MEMBER];
    private ArrayList<Programmer> team = new ArrayList<>();
    //private int total = 0;	//人数计数器

    //0
    //public Programmer[] getTeam() {
    public ArrayList<Programmer> getTeam() {
        //return team;	//此句存在重大隐患
    	
    	//返回实际人数的数组
    	//首先创建total个元素的数组
    	//然后开始循环便利，从team当中找到前面total个元素，放到实际数组里，再返回
		/*
		 * Programmer[] nowteam = new Programmer[total]; for(int i=0; i<total; i++) {
		 * nowteam[i] = team[i]; //满载数组 }
		 */
    	
    	//return nowteam;
    	
    	return team;
    }

    //1
    public void addMember(Employee e) throws TeamException {
        //如果人数超出限额，抛出异常
    	//if(total >= MAX_MEMBER) {
    	if(team.size() >= MAX_MEMBER) {
    		throw new TeamException("团队人员已满，无法添加");    		
    	}
    	
    	//如果员工不属于程序员及其子类，抛出异常
    	if(!(e instanceof Programmer)) {
    		throw new TeamException("该员工不是程序员及子类类型，无法添加");     		
    	}

        //如果状态忙碌或者休假，抛出异常
    	Programmer prog = (Programmer)e;
    	if(prog.getStatus() == Status.BUSY) {
    		throw new TeamException("该员工已在团队，不能重复添加");    
    	}
    	else if(prog.getStatus() == Status.VOCATION) {
    		throw new TeamException("该员工正在休假，不能添加"); 
    	}

    	//如果已在团队里，抛出异常
//    	for(int i=0; i<team.length; i++) {
//    		if(team[i] !=null && team[i].getId() == e.getId()) {
//    			throw new TeamException("该员工已在当前团队，不能重复添加");
//    		}
//    	} 
    	for(Programmer p : team) {
    		if(p.getId() == e.getId()) {
    			throw new TeamException("该员工已在当前团队，不能重复添加");
    		}    		
    	}
    	
    	//程序员、设计师、架构师数量超出3、2、1个，抛出异常
    	//判断顺序是否可以更换？
    	int num1=0, num2=0, num3=0;
//    	for(int i=0; i<team.length; i++) {
//    		if(team[i] instanceof Architect) {
//    			num3++;
//    		}
//    		else if(team[i] instanceof Designer) {
//    			num2++;
//    		}
//    		else if(team[i] instanceof Programmer) {
//    			num1++;
//    		}
//    	}    	
    	for(Programmer p : team) {
    		if(p instanceof Architect) {
    			num3++;
    		}
    		else if(p instanceof Designer) {
    			num2++;
    		}
    		else if(p instanceof Programmer) {
    			num1++;
    		}
    	}    	
        
    	if(prog instanceof Architect) {
    		if(num3 == 1) {
    			throw new TeamException("团队已经有1名架构师，不能添加");
    		}    		
    	}
    	else if(prog instanceof Designer) {
    		if(num2 == 2) {
    			throw new TeamException("团队已经有2名设计师，不能添加");
    		}
    	}
    	else if(prog instanceof Programmer) {
    		if(num1 == 3) {
    			throw new TeamException("团队已经有3名程序员，不能添加");
    		}
    	}
    	
    	//设置状态为忙碌，设置团队id，计数器自加1，放入数组
    	prog.setStatus(Status.BUSY);
    	
    	prog.setMemberId(counter);
    	counter++;
    	
    	//team[total] = prog;
    	team.add(prog);
    	
    	//total = total + 1;    	    	
    }

	/*
	 * private boolean isExist(Programmer p) { //判断某员工是否在团队里 }
	 */

    //2
    public void removeMember(int memberId) throws TeamException {
        //如果找不到，抛出异常
    	//否则设置状态为空闲，退出循环
    	//1
		/*
		 * int i=0; for(i=0; i<total; i++) { if(team[i].getMemberId() == memberId) {
		 * team[i].setStatus(Status.FREE); break; } }
		 * 
		 * if(i == total) { throw new TeamException("团队里查无此人，无法删除"); }
		 */
    	
    	//2
    	int index = 0;
    	boolean flag = false;
    	//for(int i=0; i<total; i++) {
    	for(int i=0; i<team.size(); i++) {
    		//if(team[i].getMemberId() == memberId) {
    			//team[i].setStatus(Status.FREE);
    		if(team.get(i).getMemberId() == memberId) {
    			team.get(i).setStatus(Status.FREE);
    			flag = true;
    			index = i;
    			break;
    		}
    	}
    	
    	if(flag == false) {
    		 throw new TeamException("团队里查无此人，无法删除"); 
    	}
    	
        //元素依次前移，然后将最后一个元素置空
//    	for(int i= index+1; i<total; i++) {
//    		team[i-1] = team[i];
//    	}
//    	team[total-1] = null;
    	team.remove(index);
    	
    	//人数计数器-1
    	//total = total - 1;	//total--;
    	
    	//team[--total] = null;
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TeamServiceColl teamService = new TeamServiceColl();
		
		PC pc1 = new PC("戴尔", "NEC17寸");
		Designer desi1 = new Designer(5, "周芷若", 28, 10000, 6, Status.FREE, pc1, 5000);
		Architect arch1 = new Architect(2, "令狐冲", 32, 18000, 9, Status.FREE, pc1, 15000, 2000);
		
		try {
			teamService.addMember(desi1);
			teamService.addMember(arch1);
			//teamService.addMember(arch1);
			System.out.println(teamService.team.size());
			
			teamService.removeMember(1);
			System.out.println(teamService.team.size());
		} 
		catch (TeamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
